﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TotalCrytography.Protector;
using System.Collections;
using TotalCrytography.Data;

namespace TotalCrytography
{
    public partial class CheckGUI : Form
    {
        private List<String> m_lstFiles = new List<string>();

        public CheckGUI()
        {
            InitializeComponent();
        }

        public CheckGUI(List<String> lstSelectedFiles)
        {
            InitializeComponent();
            txtPath.Text = lstSelectedFiles[0].Substring(0, lstSelectedFiles[0].LastIndexOf('\\') + 1);
            for (int i = 0; i < lstSelectedFiles.Count; i++)
            {
                if (File.Exists(lstSelectedFiles[i]))
                {
                    lstFiles.Items.Add(new ListViewItem(lstSelectedFiles[i]));
                    m_lstFiles.Add(lstSelectedFiles[i]);
                }
            }
        }

        private void ShowList()
        {
            lstFiles.Items.Clear();
            for (int i = 0; i < m_lstFiles.Count; i++)
            {
                if (File.Exists(m_lstFiles[i]))
                    lstFiles.Items.Add(new ListViewItem(m_lstFiles[i]));
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
                txtPath.Text = fd.SelectedPath + "\\";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to remove these files?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                foreach (ListViewItem item in lstFiles.SelectedItems)
                {
                    m_lstFiles.Remove(item.Text);
                }
                ShowList();
            }
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            List<String> lstSelectFile = m_lstFiles;
            for (int i = 0; i < lstSelectFile.Count; i++)
            {
                String inputFile = lstSelectFile[i];
                String inputSign = txtPath.Text + lstSelectFile[0].Substring(lstSelectFile[0].LastIndexOf('\\') + 1) + ".sig";
                FileStream fsread = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                StreamReader sr1 = new StreamReader(fsread);
                string strInput = sr1.ReadToEnd();
                sr1.Close();
                fsread.Close();
                string strSign = "";
                try
                {
                    StreamReader sr2 = new StreamReader(inputSign);
                    strSign = sr2.ReadToEnd();
                    sr2.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(inputFile + " is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    continue;
                }
                ArrayList lstUser = UserBUS.GetListUsers();
                bool isvalid = false;
                for (int j = 0; j < lstUser.Count; j++)
                {
                    if (eSignature.checkSignature(strInput, "SHA256", ((UserDTO)lstUser[j]).PublicKey, strSign))
                    {
                        MessageBox.Show(inputFile + " is signed by user who has email: " + ((UserDTO)lstUser[j]).Email + "!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        isvalid = true;
                        break;
                    }
                }
                if (!isvalid)
                    MessageBox.Show(inputFile + " is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        
    }
}
